package udpv2

import (
	"fmt"
	"net"
	"os"
	"strconv"
	"strings"
)

func StartUDPServer() {
	address := ServerIp + ":" + strconv.Itoa(ServerPort)

	addr, err := net.ResolveUDPAddr("udp", address)

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	conn, err := net.ListenUDP("udp", addr)

	defer conn.Close()

	for {
		data := make([]byte, ServerRecvLen)

		var1, rAddr, err := conn.ReadFromUDP(data)

		if err != nil {
			fmt.Println(err)
			continue
		}

		strRet := string(data)

		fmt.Println("var1: ", var1, "Received: ", strRet, "rAddr: ", rAddr)

		upper := strings.ToUpper(strRet)
		_, err = conn.WriteToUDP([]byte(upper), rAddr)
		if err != nil {
			fmt.Println(err)
			continue
		}

		fmt.Println("Send:", upper)

	}
}
